<template>
  <div>
    <div style="margin-bottom: 10px;margin-top: 10px;">
          <el-input v-model="createUserName"  placeholder="请输入姓名" suffix-icon="el-icon-search" style="width: 200px"
          @keyup.enter.native="loadPost"></el-input>
          <el-select v-model="status" placeholder="请选择状态" style="margin-left: 10px">
           <el-option
            v-for="item in statuss"
            :key="item.value"
            :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <!-- 会员 -->

          <el-select style="margin-left: 10px" v-model="role" filterable placeholder="请选择">
              <el-option
                      v-for="item in roles"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
              </el-option>
          </el-select>
     
          <el-button type="primary" style="margin-left: 10px" @click="loadPost">查询</el-button>
          <el-button type="success" @click="resetParam">重置</el-button>
          <el-button type="primary" style="margin-left: 10px" @click="add">新增</el-button>
      </div>
  <el-table :data="tableData"
  :header-cell-style="{background:'#f2f5fc',color:'#555'}" border>
      <el-table-column prop="id" label="ID" width="160">
      </el-table-column>

      <el-table-column prop="name" label="成果名" width="160">
      </el-table-column>
 
      <el-table-column prop="role" label="类别"  width="160" :formatter="roleFormate">
      </el-table-column>


      <el-table-column prop="status" label="状态"  width="160" :formatter="statusFormate">
      </el-table-column>

      
      <el-table-column prop="createUserName" label="创建人姓名"  width="160">
      </el-table-column>

      <el-table-column prop="createTime" label="创建时间"  width="160">
      </el-table-column>

      <el-table-column prop="updateUserName" label="修改人姓名"  width="160">
      </el-table-column>

      <el-table-column prop="updateTime" label="修改时间"  width="160">
      </el-table-column>
      
      <el-table-column prop="operate" label="操作">
          <template slot-scope="scope">
          <el-button size="small" type="success" @click="mod(scope.row.id)">编辑</el-button>
          <el-button size="small" type="success" @click="dowm(scope.row)">导出</el-button>
          <el-popconfirm
                      style="margin-left: 5px;"
                      title="这是一段内容确定删除吗？"
                      @confirm="del(scope.row.id)"
              >
                  <el-button slot="reference" size="small" type="danger">删除</el-button>
              </el-popconfirm>
          </template>
      </el-table-column>

  </el-table>
      <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pagenum"
              :page-sizes="[2, 5, 10, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
      </el-pagination>

      <el-dialog
              title="提示"
              :visible.sync="centerDialogVisible"
              width="80%"
              center>
<!--           //增加表单-->
          <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
          
            
            <el-form-item label="成果类型">
                  <el-select v-model="form.role" placeholder="请选择">
                  <el-option label="个人" value="1"></el-option>
                  <el-option label="机构" value="2"></el-option>
                   </el-select>
                </el-form-item>

            

              <el-form-item label="成果名" prop="name">
                      <el-input type="textarea" v-model="form.name"></el-input>
              </el-form-item>
<!-- 富文本编辑框 -->
              <el-form-item label="描述" prop="content">
                <WangEditor @sendEditor="sendEditor" ref="myEditor"></WangEditor>
              </el-form-item>

            
             

              <el-form-item label="附件" prop="filePath">
                <!-- 上传文件 -->
                <el-upload
                     class="upload-demo"
                     action="http://localhost:9001/jk/admin/achievement/uploadeditor"
                    :data={id:id}
                    :on-change="handleChange"
                    :limit="1"
                    :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
            <span style="margin-left:20px">{{form.filePath}}</span>
            </el-upload>
            </el-form-item>
          </el-form>

          <span slot="footer" class="dialog-footer">
            <el-button @click="centerDialogVisible = false">取 消</el-button>
           <el-button type="primary" @click="save">确 定</el-button>
            </span>

      </el-dialog>
  </div>
</template>

<script>
import WangEditor from '@/views/editor/wangEditor.vue'
    export default {
        components:{
            WangEditor,
           
        },
        name: "AchievementList",
        data() {
         
            return {
                // 查询下拉框
                statuss: [{
                value: -1,
                label: '全部状态'
                }, {
                value: 1,
                label: '申请中'
                 }, {
            value: 2,
            label: '审核通过'
            }, {
            value: 3,
            label: '审核未通过'
            }],
            status: -1,
   
            roles:[{
                value: -1,
                label: '机构+个人'
                }, {
                value: 1,
                label: '个人'
                },
                {
              value: 2,
              label: '机构'
                }],
            role:-1,
            createUserName:'',
             tableData: [],
            pagenum:1,
            pageSize:2,
            total:0,
            name:'',
            fileList:[],
            centerDialogVisible:false,
            MStype:1,
            id:'',
            session:'',
            form:{
                    id:'',
                    name:'',
                    role:1,
                    content:'',
                    filePath:'',
                    createUserId:'',
                    createUserName:''
                 
                },
                rules: {
                   
                    name: [
                        { required: true, message: '请输入名字', trigger: 'blur' },
                    ],
                   

                }

            }

        },
        created(){
            this.session = JSON.parse(sessionStorage.getItem('CurUser'))
            this.form.createUserId=this.session.createUserId
            this.form.createUserName=this.session.createUserName
           
        },
        methods:{
            sendEditor(val){
                this.form.content = val
                console.log(val)
            },
            roleFormate(row,index){
                if(row.role == 1){
                    return "个人";
                }else if(row.role == 2){
                    return "机构";
                }
            },
              // 显示状态
              statusFormate(row, index){
                if(row.status == 1){
                    return "申请中";
                }else if(row.status == 2){
                    return "审核通过";
                }else if(row.status == 3){
                    return "审核未通过";
                }
            },
            resetForm() {
                this.$refs.form.resetFields();
            },
            mod(id){
                 
                this.$axios.get(this.$httpUrl+'/jk/admin/achievement/edit?id='+id).then(res=>res.data).then(res=>{
                    this.MStype=2
                    if (res.resCode=="000000"){
                        this.centerDialogVisible=true
                this.$nextTick(()=>{
                    this.id=res.data.id
                    this.form.id = res.data.id
                    this.form.name = res.data.name
                    this.form.content = res.data.content
                    this.form.role = res.data.role
                    this.form.filePath = res.data.filePath
                    console.log(res.data.content)
                    this.$refs.myEditor.editor.txt.html(res.data.content)
                 
                    //    这个在第一次的时候不能使用，所以需要nextTick异步
                })
                    }else{
                    this.$message({
                    message: '操作失败',
                    type: 'error'
                });
                        }
                    })
              
            },
            // 删除
            del(id){
                this.$axios.delete(this.$httpUrl+'/jk/admin/achievement/remove?id='+id).then(res=>res.data).then(res=>{

                    if (res.resCode=="000000"){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面
                        this.loadPost()
                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })

            },
            add(){

                this.centerDialogVisible=true
                this.MStype=1
                this.$nextTick(()=>{
                    this.resetForm()
                //    这个在第一次的时候不能使用，所以需要nextTick异步
                })
            },
            // 修改
            doMod(){
                this.$axios.put(this.$httpUrl+'/jk/admin/achievement/update',
                    this.form

                ).then(res=>res.data).then(res=>{

                    if (res.resCode=="000000"){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面

                        this.centerDialogVisible=false
                        this.loadPost()
                        // this.resetForm()

                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })
            },
            // 增加保存
            doSave(){
                this.$axios.post(this.$httpUrl+'/jk/admin/achievement/add',
                    this.form

                ).then(res=>res.data).then(res=>{

                    if (res.resCode=='000000'){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面

                        this.centerDialogVisible=false
                        this.loadPost()
                        // this.resetForm()

                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })
            },
            save(){
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        if (this.MStype==2){
                            
                            this.doMod()
                        }else{
                           
                            this.doSave()
                        }

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },
            handleSizeChange(val) {
                
                this.pagenum=1
                this.pageSize=val
                this.loadPost()
            },
            handleCurrentChange(val) {
                
                this.pagenum=val
                this.loadPost()
            },
            handleChange(file, fileList) {
                this.form.filePath=file.response
                var a=""+this.form.filePath
                this.form.id = a.slice(10,29)
                this.fileList = fileList.slice(-3);
            },
            resetParam(){
                this.name='',
                this.isVip=-1,
                this.status=-1
                
            },
            loadPost(){
             
             this.$axios.get(this.$httpUrl+'/jk/admin/achievement/list?status='+this.status+'&createUserName='+this.createUserName+'&role='+this.role+'&pagenum='+this.pagenum +'&pageSize='+this.pageSize
             ).then(res=>res.data).then(res=>{
                
               console.log(res)
                 if (res.resCode=='000000'){
                     this.tableData=res.data.records
                     this.total=res.data.total

                 }else{
                     alert('获取数据失败')
                 }

             })
         },
         dowm(row){
                console.log(row.filePath)
                if(row.filePath!='-'){ window.location = "http://localhost:9001/jk/admin/achievement/down?id=" + row.id;}
               else{

                this.$message({
                              message: '没有附件',
                              type: 'error'
                          });
               }
            
            },
        },
        beforeMount() {
            
            this.loadPost();

        }
    }
</script>

<style>

</style>